package cn.chuanw.db.service;

import com.github.pagehelper.PageHelper;

import cn.chuanw.db.dao.*;
import cn.chuanw.db.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.util.List;

@Service
public class TenantService {
	@Resource
	private WTenantInfoMapper wTenantInfoMapper;

	@Resource
	private WRoomInfoMapper wRoomInfoMapper;

	@Resource
	private WHouseInfoMapper wHouseInfoMapper;

	@Resource
	private WIncomerentRecordMapper wIncomerentRecordMapper;

	public List<WTenantInfo> querySelective(String tenantName, String houseId, String roomId, Integer tenantStauts,
			Integer payrentStatus, LocalDate payDate, Integer page, Integer size) {
		WTenantInfoExample wTenantInfoExample = new WTenantInfoExample();
		WTenantInfoExample.Criteria criteria = wTenantInfoExample.createCriteria();
		if (!StringUtils.isEmpty(tenantName)) {
			criteria.andTenantNameLike("%" + tenantName + "%");
		}
		if (!StringUtils.isEmpty(houseId)) {
			criteria.andHouseIdEqualTo(houseId);
		}
		if (!StringUtils.isEmpty(roomId)) {
			criteria.andRoomIdEqualTo(roomId);
		}
		if (!StringUtils.isEmpty(tenantStauts)) {
			criteria.andTenantStautsEqualTo(tenantStauts);
		}
		if (!StringUtils.isEmpty(payrentStatus)) {
			criteria.andPayrentStatusEqualTo(payrentStatus);
		}
		if (!StringUtils.isEmpty(payDate)) {
			criteria.andPayDateEqualTo(payDate);
		}

		criteria.andDelFlagEqualTo(0);
		wTenantInfoExample.setOrderByClause("update_time desc");
		PageHelper.startPage(page, size);

		return wTenantInfoMapper.selectByExample(wTenantInfoExample);
	}

	public void addTenantInfo(WTenantInfo wTenantInfo) {
		WRoomInfo wRoomInfo = updateRoomInfo(wTenantInfo);
		wTenantInfo.setHouseId(wRoomInfo.getHouseId());
		wRoomInfoMapper.updateByPrimaryKeySelective(wRoomInfo);
		wTenantInfoMapper.insertSelective(wTenantInfo);
	}
	public List<WTenantInfo> selectTenantInfo(String roomId) {
		WTenantInfoExample wTenantInfoExample = new WTenantInfoExample();
		WTenantInfoExample.Criteria criteria = wTenantInfoExample.createCriteria();
		criteria.andRoomIdEqualTo(roomId).andDelFlagEqualTo(0);	
		return wTenantInfoMapper.selectByExample(wTenantInfoExample);

	}
	
	public int updateById(WTenantInfo wTenantInfo) {
		WRoomInfo wRoomInfo = updateRoomInfo(wTenantInfo);
		wTenantInfo.setHouseId(wRoomInfo.getHouseId());
		wRoomInfoMapper.updateByPrimaryKeySelective(wRoomInfo);
		return wTenantInfoMapper.updateByPrimaryKeySelective(wTenantInfo);
	}

	public List<WTenantInfo> queryAll() {
		WTenantInfoExample example = new WTenantInfoExample();
		example.createCriteria().andDelFlagEqualTo(0);
		return wTenantInfoMapper.selectByExample(example);
	}

	/**
	 * @Auther: wangkun
	 * @Date: 20-03-21 下午 5:38
	 * @Description: 更新房间状态
	 **/
	private WRoomInfo updateRoomInfo(WTenantInfo wTenantInfo) {
		String roomId = wTenantInfo.getRoomId();
		WRoomInfo wRoomInfo = wRoomInfoMapper.selectByPrimaryKey(roomId);
		WTenantInfoExample wTenantInfoExample = new WTenantInfoExample();
		wTenantInfoExample.createCriteria().andRoomIdEqualTo(roomId).andTenantStautsNotEqualTo(0);
		List<WTenantInfo> wTenantInfos = wTenantInfoMapper.selectByExample(wTenantInfoExample);
		Integer tenantStauts = wTenantInfo.getTenantStauts();
		if (tenantStauts != null) {
			if (wTenantInfos.size() == 0) {
				wRoomInfo.setLeaseStatus(tenantStauts);
			} else if (wTenantInfos.size() == 1) {
				if (wTenantInfo.getTenantName().equals(wTenantInfos.get(0).getTenantName())) { // 同一个租户
					wRoomInfo.setLeaseStatus(tenantStauts);
				}
			}
		}
		return wRoomInfo;
	}

	public WTenantInfo queryById(String tenantId) {
		return wTenantInfoMapper.selectByPrimaryKey(tenantId);
	}

	public List<WTenantInfo> playFlagUpdate() {

        WTenantInfoExample wTenantInfoExample = new WTenantInfoExample();
        WTenantInfoExample.Criteria criteria = wTenantInfoExample.createCriteria();
        criteria.andDelFlagEqualTo(0);
        List<WTenantInfo> wTenantInfos = wTenantInfoMapper.selectByExample(wTenantInfoExample);
        int nowMonth = LocalDateTime.now().getMonthValue();
        int nowDay = LocalDateTime.now().getDayOfMonth();
        int nowYear = LocalDateTime.now().getYear();
        // 获取自公元元年至今所有月份
        int allMonth = nowYear * 12 + nowMonth;
        for (WTenantInfo wTenantInfo : wTenantInfos) {
        	// 判断是否退租 4表示退租，退租的不参与刷新
       if (wTenantInfo.getPayrentStatus() != 4) {
            // 获取房屋支付方式 1押一付一 2押一付二 3押一付三 4半年付 5全款付清
            int cycle = wTenantInfo.getRentCycle();
            // 判断支付时间是否为空
            if (wTenantInfo.getPayDate() != null) {
                //欠租状态
                Period next = Period.between(wTenantInfo.getPayDate(), LocalDate.now());
                //   int months = next.getMonths();
                int days = next.getDays();
                if (days > 0) {
                    wTenantInfo.setPayrentStatus(3);
                    wTenantInfoMapper.updateByPrimaryKeySelective(wTenantInfo);
                }
                // 判断交租时间为当月并且当月已经过去的时间不更新
                if (wTenantInfo.getPayDate().getMonthValue() == nowMonth
                        && wTenantInfo.getPayDate().getDayOfMonth() >= nowDay) {

                    int month = wTenantInfo.getPayDate().getMonthValue();
                    int yer = wTenantInfo.getPayDate().getYear();

                    // 获取公元元年至起租时间所有月份，
                    int beginAllMonth = yer * 12 + month;
                    if (allMonth >= beginAllMonth) {
                        // 已租所有月份
                        int pastMonth = allMonth - beginAllMonth;
                        // 余出的月份，整除说明 需要交租 押一付一绝对整除表示每月交一次，每个月都显示一次，押一付而
                        // 余一表示下个月要交租，押一付三，余几就是剩余几个月要交租
                        int surplusMonth = pastMonth % cycle;

                        // 交租状态 需要交租的改为即将交租
                        if (surplusMonth == 0) {
                            wTenantInfo.setPayrentStatus(2);
                            wTenantInfoMapper.updateByPrimaryKeySelective(wTenantInfo);
                        }

                    }

                }
                }
            }

        }

        return wTenantInfos;

    }
}
